package com.peycard.persistence.dao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.peycard.persistence.PersistenceException;
import com.peycard.persistence.dao.local.UserListDAOLocal;
import com.peycard.persistence.entities.Company;
import com.peycard.persistence.entities.UserList;
import com.peycard.persistence.entities.VincUserCompany;

/**
 * Session Bean implementation class UserListDAO
 */
@Stateless
public class UserListDAO extends GenericDAOImpl<UserList, Long> implements
		UserListDAOLocal {

	public UserListDAO() {
	}

	public UserList findByPcUser(String pcUser) throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findUserByPcUser_1");
			query.setParameter("pcuss", pcUser);
			UserList u = (UserList) query.getSingleResult();
			return u;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public UserList editUser(String user, String pwd, String name, String lastName,
			String address, String zip, String firstA, String email,
			String phone, String country, String state, String rfc,
			String curp, String age, String userAdmin, String active,
			String secretQ, String secretA, String cellphone, String sex) throws PersistenceException{
		try{
			UserList u = findByPcUser(user);
			u.setAccessFirst(firstA);
			u.setAddress(address);
			BigDecimal agebd = new BigDecimal(age);
			u.setAge(agebd);
			u.setCelTelephone(cellphone);
			u.setCountry(country);
			u.setCurp(curp);
			u.setEmail(email);
			u.setGender(sex);
			BigDecimal activebd = new BigDecimal(active);
			u.setIsActive(activebd);
			u.setLastName(lastName);
			u.setPcName(name);
			u.setPcPassword(pwd);
			BigDecimal zipbd = new BigDecimal(zip);
			u.setPostalcode(zipbd);
			u.setRfc(rfc);
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			u.setSaveData(ts);
			u.setState(state);
			u.setTelephone(phone);
			u.setUsername(userAdmin);
			UserList usr = super.update(u);
			return usr;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public UserList addUser(String user, String pwd, String name, String lastName,
			String address, String zip, String firstA, String email,
			String phone, String country, String state, String rfc,
			String curp, String age, String userAdmin, String active,
			String secretQ, String secretA, String cellphone, String sex, String idCompany) throws PersistenceException{
		try{
			UserList u = new UserList();
			u.setAccessFirst(firstA);
			u.setAddress(address);
			BigDecimal agebd = new BigDecimal(age);
			u.setAge(agebd);
			u.setCelTelephone(cellphone);
			u.setCountry(country);
			u.setCurp(curp);
			u.setEmail(email);
			u.setGender(sex);
			BigDecimal activebd = new BigDecimal(active);
			u.setIsActive(activebd);
			u.setLastName(lastName);
			u.setPcName(name);
			u.setPcPassword(pwd);
			u.setPcUser(user);
			BigDecimal zipbd = new BigDecimal(zip);
			u.setPostalcode(zipbd);
			u.setRfc(rfc);
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			u.setSaveData(ts);
			u.setState(state);
			u.setTelephone(phone);
			u.setUsername(userAdmin);
			UserList us = super.create(u);
			Company company = getEntityManager().find(Company.class, Long.parseLong(idCompany));
			VincUserCompany vinc = new VincUserCompany();
			vinc.setIdCompany(company);
			vinc.setIdUserList(us);
			getEntityManager().persist(vinc);
			return us;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public void setDown(String idListUser, String user) throws PersistenceException{
		try {
			UserList usr = super.read(Long.parseLong(idListUser));
			usr.setIsActive(BigDecimal.valueOf(0));
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			usr.setDatemodified(ts);
			super.update(usr);
		} catch (Exception e) {
			throw new PersistenceException(e);
		} 
	}
	
	@Override
	public List<UserList> getAllUserList() throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findAllUserLists_1");
			List<UserList> list = query.getResultList();
			return list;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public List<UserList> getAllUsersNV() throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findAllUSerListNV_1");
			List<UserList> list = query.getResultList();
			return list;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}

}
